package cn.schoolwow.workflow.test.module.analyzeWorkReport.query.testcase;

import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.ContextDataQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.Assert;

import java.util.Arrays;

public class WorkFlowGeneralQueryTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        workFlowInstanceQuery(flowContext);
        workFlowCarbonCopyQuery(flowContext);
        workFlowTaskQuery(flowContext);
    }

    @Override
    public String name() {
        return "工作流通用审批测试用例";
    }

    private void workFlowInstanceQuery(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowInstanceQuery.instanceNameList.add(startWorkFlowRequest.instanceName);
        workFlowInstanceQuery.statusList.add(WorkFlowInstanceStatus.Running.status);
        workFlowInstanceQuery.assignerList.add(startWorkFlowRequest.contextData.getString("firstAuditUserId"));
        workFlowInstanceQuery.initiatorList.add(startWorkFlowRequest.initiator);
        workFlowInstanceQuery.contextDataQueryList.add(new ContextDataQuery("密码", startWorkFlowRequest.contextData.getString("密码")));

        WorkFlowInstance queryWorkFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstanceQuery);
        Assert.notNull(queryWorkFlowInstance, "查询工作流实例不能为空");

        PageVo<WorkFlowInstance> pageVo = quickWorkFlow.getWorkFlowInstancePagingList(workFlowInstanceQuery,1,10);
        Assert.isTrue(pageVo.getTotalSize()>0, "查询工作流实例不能为空");
        JSONObject o = pageVo.getArray().getJSONObject(0);
        Assert.isTrue(o.containsKey("createdAt"));

    }

    private void workFlowCarbonCopyQuery(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.carbonCopyList.addAll(startWorkFlowRequest.carbonCopyList);
        WorkFlowInstance workFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstanceQuery);
        org.junit.Assert.assertNotNull(workFlowInstance);
    }

    private void workFlowTaskQuery(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList = Arrays.asList(workFlowInstance.getId());
        workFlowTaskQuery.instanceNameList = Arrays.asList(startWorkFlowRequest.instanceName);
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);
        workFlowTaskQuery.assignerList.add(startWorkFlowRequest.contextData.getString("firstAuditUserId"));
        workFlowTaskQuery.initiatorList = Arrays.asList(startWorkFlowRequest.initiator);
        workFlowTaskQuery.contextDataQueryList.add(new ContextDataQuery("密码", startWorkFlowRequest.contextData.getString("密码")));
        workFlowTaskQuery.carbonCopyList.addAll(startWorkFlowRequest.carbonCopyList);

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        Assert.notNull(workFlowTaskResponse, "查询工作流任务不能为空");

        PageVo<WorkFlowTaskResponse> workFlowTaskResponsePageVo = quickWorkFlow.getWorkFlowTaskPagingList(workFlowTaskQuery,1,10);
        Assert.isTrue(workFlowTaskResponsePageVo.getTotalSize()>0, "查询工作流任务不能为空");
    }
}
